package org.travel_system.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.travel_system.pojo.Complaint;
import org.travel_system.service.UserService;
import org.travel_system.utils.MinioUtil;
import org.travel_system.utils.Result;
import org.travel_system.service.ComplaintService;
import org.travel_system.utils.ThreadLocalUtil;

import java.util.List;
import java.util.Map;

@Controller
@RestController
@RequestMapping("/complaints")
public class ComplaintController
{
    private final UserService userService;
    private final ComplaintService complaintService;

    @Autowired
    public ComplaintController(UserService userService, ComplaintService complaintService){
        this.userService = userService;
        this.complaintService = complaintService;
    }
    @Autowired
    private MinioUtil minioUtil;
    //添加投诉
    @ResponseBody
    @PostMapping
    public Result<?> addComplaint(@RequestParam("file") MultipartFile file,
                                  @RequestParam("message") String message
                                  ) {
        try {
            // 处理文件上传，存储到服务器或云存储中
            String fileUrl = minioUtil.putObject(file); // 假设使用 minioUtil 进行文件存储

            // 获取当前用户信息
            Map<String, Object> map = ThreadLocalUtil.get();
            String username = (String) map.get("username");
            Integer userId = userService.findUserId(username);
            System.out.println(message);
            // 创建投诉对象
            Complaint complaint = new Complaint();
            complaint.setTouristId(userId);
            complaint.setIsApproved(0);
            complaint.setIsProcessed(0);
            complaint.setComplaintContent(message); // 设置投诉信息
            // 判断文件类型并设置投诉的图片或视频字段
            String fileType = complaintService.getFileTypeFromUrl(fileUrl);
            if ("image".equals(fileType)) {
                complaint.setComplaintPhoto(fileUrl);
            } else if ("video".equals(fileType)) {
                complaint.setComplaintVideo(fileUrl);
            }

            // 调用服务层方法进行投诉添加
            return complaintService.addComplaint(complaint) > 0 ?
                    Result.success() :
                    Result.error("添加投诉失败");

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误：" + e.getMessage());
        }
    }

    //根据投诉是否被已被审批和处理来决定是否删除
    @DeleteMapping("/{id}")
    public Result<?> deleteComplaint(@PathVariable String id)
    {
        Complaint complaint = complaintService.getComplaintById(Integer.parseInt(id));
        return (complaint.getIsApproved() == 1 || complaint.getIsProcessed() == 1) &&
                complaintService.deleteComplaint(complaint) > 0 ?
                Result.success():Result.error("投诉未处理(未审批)");
    }

    //修改审批和处理属性
    @PutMapping
    public Result<?> updateComplaint(@RequestBody Complaint complaint)
    {

        return complaintService.updateComplaint(complaint) > 0 ?
                Result.success() :
                Result.error("修改失败");
    }


    //查询用户投诉列表
    @GetMapping
    public Result<List<Complaint>> getComplaints()
    {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Integer touristId = userService.findUserId(username);
        return Result.success(complaintService.getComplaints(touristId));
    }


    //查询单个投诉
    @GetMapping("/{id}")
    public Result<Complaint> complaint(@PathVariable int id)
    {
        Complaint complaint = complaintService.getComplaintById(id);
        return Result.success(complaint);
    }

    //查询所有投诉
    @GetMapping("/all")
    public Result<List<Complaint>> getAllComplaints() {
        return Result.success(complaintService.getAllComplaints());
    }
}
